#!/bin/bash
set -e

source $(dirname $0)/version

cd $(dirname $0)/..
mkdir -p bin

OS_PLATFORM_ARG=(linux darwin windows)
OS_ARCH_ARG[linux]="amd64 arm"
OS_ARCH_ARG[windows]="386 amd64"
OS_ARCH_ARG[darwin]="amd64"

[ "$(uname)" != "Darwin" ] && LINKFLAGS="-extldflags -static -s"
CGO_ENABLED=0 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/system-tools

if [ -n "$CROSS" ]; then
  rm -rf build/bin
  mkdir -p build/bin
  for OS in ${OS_PLATFORM_ARG[@]}; do
      for ARCH in ${OS_ARCH_ARG[${OS}]}; do
          OUTPUT_BIN="build/bin/system-tools_$OS-$ARCH"
          if test "$OS" = "windows"; then
              OUTPUT_BIN="${OUTPUT_BIN}.exe"
          fi
          echo "Building binary for $OS/$ARCH..."
          GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build \
                -ldflags="-w -X main.VERSION=$VERSION" \
                -o ${OUTPUT_BIN}
      done
  done
fi
